package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();/*创建连接*/
        f.setPort(5672);
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        Channel con = f.newConnection().createChannel();

        /*创建随机队列*/
        String queue = UUID.randomUUID().toString();
        con.queueDeclare(queue,false,true,true,null);/*创建队列*/
        con.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);/*创建默认交换机*/
        /*第三个参数无效*/
        con.queueBind(queue,"logs","");

        /*从随机队列接受处理消息*/
        DeliverCallback deliverCallback= new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] body = delivery.getBody();
                String s1 = new String(body);
                System.out.println("结果为:"+s1);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };                      /*自动确认*/
        con.basicConsume(queue,true,deliverCallback,cancelCallback);/*接收数据*/
    }
}
